/**
 * @author 黄波
 * 创建日期：2011Aug 31, 2011
 * TODO// N/A
 */
package com.platform.user.bo;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.platform.bean.PageBean;
import com.platform.dao.BaseDao;
import com.platform.pojo.UserGroup;
import com.platform.pojo.UserInfo;

//用户信息bo
public class UserBo {
	private static UserBo instance;
	static {
		instance = new UserBo();
	}
	private UserBo() {
		
	}
	public static UserBo getInstance() {
		return instance;
	}

	/**
	 * 返回所有用户组信息
	 * @return
	 */
	public List<UserGroup> getGroups() {
		return BaseDao.getList("from UserGroup");
	}
	
	/**
	 * 添加用户组信息
	 * @param obj
	 * @return
	 */
	public boolean addUserGroup(UserGroup obj) {
		return BaseDao.add(obj);
	}
	
	/**
	 * 更新用户组信息
	 * @param obj
	 * @return
	 */
	public boolean updateUserGroup(UserGroup obj) {
		return BaseDao.update(obj);
	}
	
	/**
	 * 删除一个用户组
	 * @param id
	 * @return
	 */
	public boolean deleteUserGroup(String id) {
		String sql = "DELETE FROM USER_GROUP WHERE id=" + id;
		return BaseDao.executeSql(sql);
	}
	
	/**
	 * 根据ID获得一个用户组
	 * @param id
	 * @return
	 */
	public UserGroup getUserGroup(String id) {
		String hql = "from UserGroup where id=" + id;
		return (UserGroup)BaseDao.getUniqueResult(hql);
	}
	
	/**
	 * 根据pageBean信息分页查询用户信息
	 * @param pageBean
	 * @return
	 */
	public Object[] getUserList(PageBean pageBean) {
		String hql = "from UserInfo order by isAudit asc, regTime desc";
		if(pageBean == null) {
			//如传入参数为空，则使用默认的分页
			pageBean = new PageBean();
			return BaseDao.getSpiltList(pageBean.getCurrPage(), pageBean.getPageSize(), hql);
		} else {
			//使用传入的page bean 进行分页查询
			return BaseDao.getSpiltList(pageBean.getCurrPage(), pageBean.getPageSize(), hql);
		}
	}
	
	/**
	 * 添加用户信息
	 * @param user
	 * @return
	 */
	public boolean addUser(UserInfo user) {
		if(user != null && getUser1(user.getLoginName().trim()) != null) {
			return false;
		}
		return BaseDao.add(user);
	}
	
	/**
	 * 根据用户ID删除用户信息
	 * @param id
	 * @return
	 */
	public boolean delUser(String id) {
		String sql = "DELETE FROM USER_INFO WHERE id=" + id;
		return BaseDao.executeSql(sql);
	}
	
	/**
	 * 更新用户信息
	 * @param user
	 * @return
	 */
	public boolean updateUser(UserInfo user) {
		return BaseDao.update(user);
	}

	/**
	 * 根据ID获得用户详细信息
	 * @param id
	 * @return
	 */
	public UserInfo getUser(String id) {
		String hql = "from UserInfo where id=" + id;
		return (UserInfo)BaseDao.getUniqueResult(hql);
	}
	
	/**
	 * 根据用户登录名获得用户详细信息
	 * @param id
	 * @return
	 */
	public UserInfo getUser1(String loginName) {
		String hql = "from UserInfo where loginName='" + loginName + "'";
		return (UserInfo)BaseDao.getUniqueResult(hql);
	}
	
	/**
	 * 执行批量添加用户操作
	 * @param users
	 * @return
	 */
	public boolean batchAddUser(List users) {
		return BaseDao.batchSave(users);
	}
	
	/**
	 * 根据用户名和密码查询用户
	 * @param loginName
	 * @param loginPasswd
	 * @return
	 */
	public UserInfo getUser(String loginName, String loginPasswd) {
		return (UserInfo)BaseDao.getUniqueResult("from UserInfo where loginName='" + loginName + "' and loginPasswd='" + loginPasswd + "'");
	}
	
	/**
	 * 查询十位资源共享达人信息
	 * @return
	 */
	public Object[] getDRUsers() {
		return BaseDao.getSpiltList(1, 10, "from UserInfo order by shareCount desc");
	}
	

	
	/**
	 * 已注册用户数
	 * @return
	 */
	public Long countUser() {
		return (Long)BaseDao.getUniqueResult("select count(*) from UserInfo");
	}
	
	/**
	 * 共享资源最多的用户
	 * @return
	 */
	public UserInfo getDaUser() {
		Object[] objs = BaseDao.getSpiltList(1, 1, "from UserInfo order by shareCount desc");
		List list = (List) objs[1];
		if(list != null && list.size() > 0) {
			return (UserInfo)list.get(0);
		}
		return null;
	}

	/**
	 * 根据邮箱查询用户
	 * @param email
	 * @return
	 */
	public UserInfo getUserByEamil(String email) {
		String hql = "from UserInfo where email='" + email + "'";
		return (UserInfo)BaseDao.getUniqueResult(hql);
	}
	/**
	 * 根据手机号查询用户
	 * @param email
	 * @return
	 */
	public UserInfo getUserByMobile(String mobile) {
		String hql = "from UserInfo where mobile='" + mobile + "'";
		return (UserInfo)BaseDao.getUniqueResult(hql);
	}
	
	/**
	 * 根据帐号和邮箱地址查询用户
	 * @param loginName
	 * @param email
	 * @return
	 */
	public UserInfo getUserByEamil(String loginName, String email) {
		String hql = "from UserInfo where loginName='" + loginName + "' email='" + email + "'";
		return (UserInfo)BaseDao.getUniqueResult(hql);
	}
}
